package cn.gdpu.action;

import cn.gdpu.service.AdminService;
import cn.gdpu.service.GoodsService;
import cn.gdpu.service.GoodsTypeService;
import cn.gdpu.util.PageBean;
import cn.gdpu.vo.Admin;
import cn.gdpu.vo.Goods;
import cn.gdpu.vo.GoodsType;

@SuppressWarnings("serial")
public class GoodsTypeAction extends BaseAction {
	
	private GoodsService<Goods, Integer> goodsService;
	private GoodsTypeService<GoodsType, Integer> goodsTypeService;
	private AdminService<Admin, Integer> adminService;
	private Admin admin;
	private Goods goods;
	private GoodsType goodsType;
	private int id;
	private String name;
	private PageBean pageBean;
	private int page;
	
	@Override
	public String add() {
		Admin admin = (Admin) getSession().get("admin");
		if(admin != null && goodsTypeService.queryByName(name) == null){
			GoodsType goodsType1 = new GoodsType();
			goodsType1.setName(name);
			
			goodsTypeService.addEntity(goodsType1);
			return super.add();
		}else{
			return ERROR;
		}
	}

	@Override
	public String delete() {
		Admin admin = (Admin) getSession().get("admin");
		if(admin != null){
			goodsTypeService.deleteEntity(GoodsType.class, id);
			return super.delete();
		}else{
			return ERROR;
		}
	}

	@Override
	public String deleteMany() {
		// TODO Auto-generated method stub
		return super.deleteMany();
	}

	@Override
	public String goAdd() {
		return super.goAdd();
	}

	@Override
	public String goModify() {
		Admin admin = (Admin) getSession().get("admin");
		if(admin != null){
			goodsType = goodsTypeService.getEntity(GoodsType.class, id);
			return super.goModify();
		}else{
			return ERROR;
		}
	}

	@Override
	public String list() {
		Admin admin = (Admin) getSession().get("admin");
		if(admin != null){
			this.pageBean = goodsTypeService.queryForPage(GoodsType.class, 10, page);
			if(pageBean.getList().isEmpty())
	    		pageBean.setList(null);
			return super.list();
		}else{
			return ERROR;
		}
	}

	@Override
	public String modify() {
		Admin admin = (Admin) getSession().get("admin");
		if(admin != null){
			goodsType = goodsTypeService.getEntity(GoodsType.class, id);
			goodsType.setName(name);
			goodsTypeService.updateEntity(goodsType);
			return super.modify();
		}else{
			return ERROR;
		}
	}

	@Override
	public String view() {
		Admin admin = (Admin) getSession().get("admin");
		if(admin != null){
			String hql = "from Goods g where g.goodsType.id = '" + id + "'";
			this.pageBean = goodsService.queryForPage(hql, 10, page);
			if(pageBean.getList().isEmpty())
	    		pageBean.setList(null);
			return super.view();
		}else{
			return ERROR;
		}
	}
	
	//getter and setter

	public GoodsService<Goods, Integer> getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService<Goods, Integer> goodsService) {
		this.goodsService = goodsService;
	}

	public GoodsTypeService<GoodsType, Integer> getGoodsTypeService() {
		return goodsTypeService;
	}

	public void setGoodsTypeService(
			GoodsTypeService<GoodsType, Integer> goodsTypeService) {
		this.goodsTypeService = goodsTypeService;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public GoodsType getGoodsType() {
		return goodsType;
	}

	public void setGoodsType(GoodsType goodsType) {
		this.goodsType = goodsType;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public AdminService<Admin, Integer> getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService<Admin, Integer> adminService) {
		this.adminService = adminService;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

}
